package com.ukao.steward.ui.valetRunners.createOrder;


import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ukao.steward.R;
import com.ukao.steward.base.BaseFragment;
import com.ukao.steward.bean.SearchUserBean;
import com.ukao.steward.consts.Constant;
import com.ukao.steward.eventbus.ShoppEvent;
import com.ukao.steward.util.CheckUtils;
import com.ukao.steward.util.ImageUtils;
import com.ukao.steward.util.T;
import com.ukao.steward.widget.ShapedImageView;

import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import rx.Observable;
import rx.Subscriber;
import rx.functions.Func1;

/*
客戶信息
 */
public class GuestFragment extends BaseFragment {

    @BindView(R.id.back_btn)
    ImageView backBtn;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.title_layout)
    RelativeLayout titleLayout;
    @BindView(R.id.headimgPath)
    ShapedImageView headimgPath;
    @BindView(R.id.product_name)
    TextView name;
    @BindView(R.id.phone)
    TextView phone;
    @BindView(R.id.use_balance)
    TextView useBalance;
    @BindView(R.id.use_cardNo)
    TextView useCardNo;
    @BindView(R.id.use_cardLevel)
    TextView useCardLevel;
    @BindView(R.id.next_btn)
    Button nextBtn;
    Unbinder unbinder;
    @BindView(R.id.address)
    TextView address;
    @BindView(R.id.address_layout)
    LinearLayout addressLayout;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private SearchUserBean mSearchUserBean;
    private String addressId="";
    public String addressName="";
    private String addressPhone="";

    public static GuestFragment newInstance(Parcelable muserInfoCard, String param2) {
        GuestFragment fragment = new GuestFragment();
        Bundle args = new Bundle();
        args.putParcelable(ARG_PARAM1, muserInfoCard);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mSearchUserBean = getArguments().getParcelable(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_guest, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }




    @Override
    protected void initView() {
        title.setText("客户信息");
        setStatusBarTintColor(R.color.normal_blue);
        titleLayout.setBackgroundColor(getColors(R.color.colorPrimary));
        fillData();
    }


    @Override
    protected boolean useEventBus() {
        return true;
    }

    @Subscribe
    public void onEvent(ShoppEvent event) {
        switch (event.getMessage()) {
            case UPDATE_ADDRESS:/*更新地址*/
                List<SearchUserBean.AddressListBean >  mdata=(List<SearchUserBean.AddressListBean>)event.getData();
                if (CheckUtils.isNull(mSearchUserBean))return;
                mSearchUserBean.setAddressList(mdata);
                break;
            case FINISH:
                finish();
                break;



        }
    }



    private void fillData() {
        if (!CheckUtils.isNull(mSearchUserBean)) {
            useBalance.setText("￥" + CheckUtils.isEmptyNumber(mSearchUserBean.getBalance()));
            useCardLevel.setText(CheckUtils.isEmptyString(mSearchUserBean.getCardName()));
            useCardNo.setText(CheckUtils.isEmptyString(mSearchUserBean.getCardNo()));
            name.setText(CheckUtils.isEmptyString(mSearchUserBean.getName()));
            phone.setText(CheckUtils.isEmptyString(mSearchUserBean.getPhone()));
            ImageUtils.loadHeadImage(_mActivity, mSearchUserBean.getHeadimgPath(), headimgPath);
            if (CheckUtils.isNull(mSearchUserBean.getAddressList()))return;
            Observable.from(mSearchUserBean.getAddressList()).filter(new Func1<SearchUserBean.AddressListBean, Boolean>() {
                @Override
                public Boolean call(SearchUserBean.AddressListBean addressListBean) {
                    return addressListBean.getDef()==1;
                }
            }).subscribe(new Subscriber<SearchUserBean.AddressListBean>() {


                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(SearchUserBean.AddressListBean addressListBean) {
                    addressId=addressListBean.getId();
                     addressName=addressListBean.getName();
                    addressPhone=addressListBean.getPhone();
                    address.setText(addressListBean.getRegionalDesc()+addressListBean.getDesc());
                }
            });


        }


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.back_btn, R.id.next_btn, R.id.address_layout,R.id.easy_next_btn,R.id.incoming_orders_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();

                break;
            case R.id.address_layout:
                if (CheckUtils.isNull(mSearchUserBean))return;
                String  strName=CheckUtils.isEmpty(addressName)?mSearchUserBean.getName():addressName;
                String  strPhone=CheckUtils.isEmpty(addressPhone)?mSearchUserBean.getPhone():addressPhone;
                AddressIdListFragment mAddressFragment = AddressIdListFragment.newInstance( strName,strPhone,String.valueOf(mSearchUserBean.getUserId()));
               startForResult(mAddressFragment, Constant.REQUEST_CODE);
                break;
            case R.id.next_btn:
                if (CheckUtils.isNull(mSearchUserBean)) return;
                if (CheckUtils.isEmpty(mSearchUserBean.getAddressList())){
                    T.show("请添加下单地址");
                return;}
                start(shoppingMallFragment.newInstance(String.valueOf(mSearchUserBean.getUserId()),addressId,addressPhone,addressName,mSearchUserBean.getCardDiscount(),false));
                break;
            case  R.id.incoming_orders_btn:
                if (CheckUtils.isNull(mSearchUserBean)) return;
                if (CheckUtils.isEmpty(mSearchUserBean.getAddressList())){
                    T.show("请添加下单地址");
                    return;}
                start(shoppingMallFragment.newInstance(String.valueOf(mSearchUserBean.getUserId()),addressId,addressPhone,addressName,mSearchUserBean.getCardDiscount(),true));
                break;

            case  R.id.easy_next_btn:
                if (CheckUtils.isNull(mSearchUserBean)) return;
                if (CheckUtils.isEmpty(mSearchUserBean.getAddressList())){
                    T.show("请添加下单地址");
                    return;}

                start(EasyOrederFragment.newInstance( addressId,  String.valueOf(mSearchUserBean.getUserId()),  addressName ,  addressPhone));

                break;
        }
    }




    @Override
    public void onFragmentResult(int requestCode, int resultCode, Bundle data) {
        super.onFragmentResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case Constant.REQUEST_CODE:
                    SearchUserBean.AddressListBean mItem=data.getParcelable(ARG_PARAM1);
                    addressPhone=mItem.getPhone();
                    addressId=mItem.getId();
                    addressName=mItem.getName();
                    address.setText(mItem.getRegionalDesc()+mItem.getDesc());
                    break;
            }
        }

    }

}
